package org.soar.views;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import org.soar.SoarApplication;
import org.soar.SoarContainer;
import org.soar.helper.SoarHelper;
import org.soar.views.listener.ActivityResultListener;

public class SoarActivity extends Activity {
	
	private List<ActivityResultListener> activityResultListeners = new ArrayList();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		SoarHelper.processOnCreateActivity(this);
		
	}
	
	public Map getDataMap(){
		Bundle extras = getIntent().getExtras();
		Map dataMap = null;
		
		if(extras != null){
			for(Iterator ite = extras.keySet().iterator(); ite.hasNext(); ){
				String name = (String) ite.next();
				Object value = extras.get(name);
				if(dataMap == null){
					dataMap = new HashMap();
				}
				dataMap.put("name", value);
			}
		}
		return dataMap;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(activityResultListeners.size() > 0){
			for(ActivityResultListener activityResultListener : activityResultListeners){
				activityResultListener.onResult(requestCode, resultCode, data);
			}
		}
		
	}
	
	public void registerActivityResultListener(ActivityResultListener activityResultListener){
		activityResultListeners.add(activityResultListener);
	}
	
	public void removeActivityResultListener(ActivityResultListener activityResultListener){
		activityResultListeners.remove(activityResultListener);
	}
	
	@Override
	protected void onResume() {
		//HACK: not sure if worked for Activity groups
		SoarContainer soar = ((SoarApplication)getApplicationContext()).getSoarContainer();
		soar.processActivityContext(this);
		
		super.onResume();
	}
}
